﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MyOA.Model;


namespace MyOA.DAL
{
    public class FileMainService
    {
        DBHelper db = new DBHelper();

        /// <summary>
        /// 根据parentId来获取文件信息
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns>文件信息</returns>
        public IList<FileInfo> getFileInfoByParentId(int parentId)
        {
            IList<FileInfo> fileInfos = new List<FileInfo>();
            using (SqlDataReader reader = db.getDataReader("sp_getFileInfoByParentId", new SqlParameter("@parentId", parentId)))
            {
                while (reader.Read())
                {
                    FileInfo fileInfo = new FileInfo();
                    fileInfo.FileId = (int)reader[0];
                    fileInfo.FilesName = (string)reader[1];
                    fileInfo.ParentId = (int)reader[2];
                    fileInfo.FileType.FileTypeImage = (string)reader[3];
                    fileInfo.FileType.FileTypeName = (string)reader[4];
                    fileInfo.Remark = (string)reader[5];
                    fileInfo.FileOwner.UserName = (string)reader[6];
                    fileInfo.CreateDate = (DateTime)reader[7];
                    fileInfo.FileType.FileTypeId = (int)reader[8];
                    fileInfo.FilePath = (string)reader[9];
                    fileInfos.Add(fileInfo);
                }
            }
            db.CloseConnection();
            return fileInfos;
        }

        /// <summary>
        /// 根据文件ID找此文件的路径
        /// </summary>
        /// <param name="fileId">文件ID</param>
        /// <returns>文件路径</returns>
        public string getFilePathByFileId(int fileId)
        {
            return (string)db.getScalar("sp_getFilePathByFileId", new SqlParameter("@fileId", fileId));
        }

        /// <summary>
        /// 根据文件ID 来修改文件信息表中的是否删除那个字段
        /// </summary>
        /// <param name="fileId">文件ID</param>
        /// <returns>是否修改成功</returns>
        public bool updateIfDeleteByFileId(int fileId)
        {
            if (db.getCount("sp_updateIfDeleteByFileId", new SqlParameter("@fileId", fileId)) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据文件ID找文件类型ID
        /// </summary>
        /// <param name="fileId">文件ID</param>
        /// <returns>文件类型ID</returns>
        public int getFileTypeIdByFileId(int fileId)
        {
            return (int)db.getScalar("sp_getFileTypeIdByFileId", new SqlParameter("@fileId", fileId));
        }
    }
}
